package com.yanh.transaction.controller;


import com.yanh.common.exception.BusinessException;
import com.yanh.transaction.dto.PayChannelDTO;
import com.yanh.transaction.dto.PayChannelParamDTO;
import com.yanh.transaction.service.IPlatformChannelService;
import com.yanh.transaction.vo.PlatformChannelVo;
import com.yanh.transaction.service.IPayChannelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yanh
 * @since 2024-06-02
 */

@RestController
@RequiredArgsConstructor
@Tag(name="平台服务管理接口",description = "平台服务管理接口")
@RequestMapping("/pay-channel")
public class PayChannelController {

    private final IPayChannelService payChannelService;
    private final IPlatformChannelService platformChannelService;

    @Operation(summary = "获取平台服务类型")
    @GetMapping(value="/transaction/platform-channels")
    public List<PlatformChannelVo> queryPlatformChannel(){
        return payChannelService.queryPlatformChannel();
    }

    @Operation(summary ="应用绑定服务类型")
    @Parameters(value = {@Parameter(name = "appId", description = "应用id", example = "1")})
    @PostMapping(value="/transaction/apps/{appId}/platform-channels")
    public boolean bindPlatformForApp(@PathVariable("appId") String appId, @RequestParam("platformChannelCodes") String platformChannelCodes){
        return payChannelService.bindPlatformChannelForApp(appId,platformChannelCodes);
    }

    @Operation(summary ="查询应用是否绑定了某个服务类型")
    @Parameters(value = {@Parameter(name = "appId", description = "应用id", example = "1"),
            @Parameter(name = "platformChannel", description = "服务类型", example = "1")})
    @GetMapping("/transaction/merchants/apps/platformchannels")
    public boolean queryAppBindPlatformChannel(@RequestParam String appId, @RequestParam String platformChannel){
        return payChannelService.queryAppBindPlatformChannel(appId,platformChannel);
    }

    @Operation(summary ="根据服务类型查询支付渠道")
    @GetMapping(value="/transaction/pay-channels/platform-channel/{platformChannelCode}")
    public List<PayChannelDTO> queryPayChannelByPlatformChannel(@PathVariable("platformChannelCode") String platformChannelCode) throws BusinessException {
        return platformChannelService.selectPayChannelByPlatformChannel(platformChannelCode);

    }

    @Operation(summary ="商户配置支付渠道参数")
    @RequestMapping(value = "/transaction/pay-channel-params",method = {RequestMethod.POST,RequestMethod.PUT})
    public void createPayChannelParam(@RequestBody PayChannelParamDTO payChannelParamDTO){
        if(payChannelParamDTO.getChannelName() == null){
            throw new BusinessException("");
        }
        if(payChannelParamDTO.getPayChannel() == null){
            throw new BusinessException("");
        }
        if(payChannelParamDTO.getPlatformChannelCode() == null){
            throw new BusinessException("");
        }
        if(payChannelParamDTO.getMerchantId() == null){
            throw new BusinessException("");
        }
        payChannelService.savePayChannelParam(payChannelParamDTO);
    }

    @Operation(summary ="应用和服务类型获取支付渠道参数列表")
    @Parameters(value = {@Parameter(name = "appId", description = "应用id", example = "1"),@Parameter(name = "platformChannel", description = "服务类型代码", example = "1")})
    @GetMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}")
    public  List<PayChannelParamDTO> queryPayChannelParam(@PathVariable("appId")String appId,@PathVariable("platformChannel")String platformChannel){
        return payChannelService.queryPayChannelParamByAppAndPlatform(appId,platformChannel);
    }

    @Operation(summary ="应用和服务类型和支付渠道获取单个支付渠道参数")
    @Parameters(value = {@Parameter(name = "appId", description = "应用id", example = "1"),
            @Parameter(name = "platformChannel", description = "服务类型代码", example = "1"),
            @Parameter(name = "payChannel", description = "支付渠道代码", example = "1")
    })
    @GetMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}/pay-channels/{payChannel}")
    public  PayChannelParamDTO queryPayChannelParam(@PathVariable("appId")String appId,@PathVariable("platformChannel")String platformChannel,@PathVariable("payChannel") String payChannel){
        return payChannelService.queryParamByAppPlatformAndPayChannel(appId,platformChannel,payChannel);
    }
}
